 	program testefcn

        include 'param_dim.inc'

      	integer m,numpar,ldfjac	
	integer maxfev,mode,nprint,info,nfev,njev
	integer ipvt(npar_max)
	double precision ftol,xtol,gtol,factor

	double precision p(npar_max),s(npar_max)
	double precision x(npar_max),fvec(NRCX),fjac(NRCX,npar_max),
     *        diag(npar_max), qtf(npar_max), wa1(npar_max),
     *        wa2(npar_max), wa3(npar_max), wa4(NRCX)
	double precision epsfcn

        double precision fvec1(NRCX)

	double precision fjacDD(NRCX*npar_max),fjacDF(NRCX*npar_max)
	double precision errodif(npar_max),normadif


        double precision r,dudp(Nparg_max),h
	integer count1,count2,count_rate,count_max

	integer FDAT,FDCC,FDEX,FRES,FROT
	CHARACTER*11 FILEB
	CHARACTER*15 FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
	character*18 FILEPLD

	common/geom/p,s,numpc
	COMMON /narq/ FDAT,FDCC,FDEX,FRES,FROT
	COMMON /cFDAT/ FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
! 	external fcn
	external fcn1,fcn2


	call input_opt(ftol,xtol,gtol,maxfev,numpar,
     &           m,metodo,mode,nprint,factor,epsfcn)
	ldfjac = NRCX


	if(numpar .eq. numpc)then
            do i=1,numpar
                !x(i)=s(i)
                x(i)=tan((s(i)-0.5)*3.1416)
            enddo
	else if(numpar .eq. 2*numpc)then
            do i=1,numpar
                x(i)=p(i)
            enddo
	else if(numpar .eq. 3*numpc)then
            do i=1,numpc
                x(3*i-2)=p(2*i-1)
                x(3*i-1)=p( 2*i )
                !x(3*i  )=s(  i  )
                x(3*i  )=tan((s(i)-0.5)*3.1416)
            enddo
	endif





! ! Analise da influencia do epsfcn na norma do vetor (fvec1-fvec)/h
! 
!         iflag = 1
!         metodo = 1
!         
!         r = RESID(x,fvec,fjac,m,numpar,m,iflag,metodo)
!         
! 	iflag = 2
!         metodo = 2
!         
!         r = RESID(x,fvec,fjacDD,m,numpar,m,iflag,metodo)       
! 
! 	open(1,file='hj-norma-jacDF.txt')
! 	open(2,file='hj-norma-erro-jac.txt')
!         iflag = 1
!         metodo = 1        
!         do k=0,28
! 
!             epsfcn = 2.**(-12.+k*.5)
! 
!             h = dsqrt(epsfcn)
!             
!             do j = 1, numpar
!                 temp = x(j)
!                 
!                 x(j) = temp + h
!                 r = RESID(x,fvec1,fjac,m,numpar,m,iflag,metodo)
!                 x(j) = temp
!                 
!                 dudp(j) = 0.
! 		errodif(j) = 0.
! 		normadif = 0.
!                 do i = 1, m
!                     dudp(j) = dudp(j) + ((fvec1(i) - fvec(i))/h)**2
! 		    fjacDF((j-1)*m+i) = (fvec1(i) - fvec(i))/h	
! 		    errodif(j) = errodif(j) + (fjacDD((j-1)*m+i) - 
!      &					fjacDF((j-1)*m+i))**2
! 		    normadif = normadif + fjacDD((j-1)*m+i)**2
!                 enddo
!                 dudp(j) = dsqrt(dudp(j))
!     		errodif(j) = dsqrt(errodif(j))/normadif
!             enddo
!         
!             write(1,6)epsfcn,(dudp(j),j=1,numpar)
! 	    write(2,6)epsfcn,(errodif(j),j=1,numpar)
!         enddo
! 	close(1)
! 	close(2)
! 
!  6      format(f14.8,12f12.6)
!         stop
! !   fim da analise da influencia do epsfcn no calculo do jacobiano



	if(metodo.eq.1)then

            write(4,*)'_______________________________________'
	    write(4,*)'--L-M - J:Finite Differences--'
            write(4,*)'_______________________________________'

            call system_clock(count1,count_rate,count_max)

	    call lmdifmodif(fcn1,m,numpar,x,fvec,ftol,xtol,gtol,maxfev,
     *            epsfcn,diag,mode,factor,nprint,info,nfev,fjac,ldfjac,
     *                ipvt,qtf,wa1,wa2,wa3,wa4)
            call system_clock(count2,count_rate,count_max)
            r=(count2-count1)/count_rate

            write(4,*)'Fim da primeira etapa da otimizacao'
            write(4,*)'Tempo de resol. (1a etapa) c/ LMDIF:',r,' s'
            write(4,*)'info: ',info,'  nfev: ',nfev
	else if(metodo.eq.2)then

            write(4,*)'_______________________________________'
            write(4,*)'--L-M - J:DirectDifferentiation--'
            write(4,*)'_______________________________________'

            call system_clock(count1,count_rate,count_max)

            call lmder(fcn2,m,numpar,x,fvec,fjac,ldfjac,ftol,xtol,gtol,
     *                 maxfev,diag,mode,factor,nprint,info,nfev,njev,
     *                 ipvt,qtf,wa1,wa2,wa3,wa4)
            call system_clock(count2,count_rate,count_max)
            r=(count2-count1)/count_rate

            write(4,*)'Tempo de resol. (1a etapa) c/ LMDER:',r,' s'
            write(4,*)'info: ',info,'  nfev: ',nfev,'  njev:',njev
	endif




        !  2o passo da otimizacao   - 
	if(numpar .eq. 2*numpc)then
            write(4,*)'************************************'
            write(4,*)' 2o passo da otimizacao - x y s '
            write(4,*)'************************************'

!             xtol = xtol/2.
!             ftol = ftol/2.
!             gtol = gtol/2.

            do i=1,numpar
                p(i)=x(i)
            enddo
            
	
            numpar = 3*numpc

		write(4,*)
		write(4,*)'  Parametros do alg. de otimizacao'
		write(4,*)
		write(4,*)'   ftol: ',ftol
		write(4,*)'   xtol: ',xtol
		write(4,*)'   gtol: ',gtol
		write(4,*)'   maxfev: ',maxfev
		write(4,*)'   numpar: ',numpar		

            do i=1,numpc
                x(3*i-2)=p(2*i-1)
                x(3*i-1)=p( 2*i )
                x(3*i  )=tan((s(i)-0.5)*3.1416)
            enddo
    
            if(metodo.eq.1)then

                write(4,*)'_______________________________________'
                write(4,*)'--L-M - J:Finite Differences--'
                write(4,*)'_______________________________________'


                call system_clock(count1,count_rate,count_max)

                call lmdifmodif(fcn1,m,numpar,x,fvec,ftol,xtol,gtol,
     *                   maxfev,epsfcn,diag,mode,factor,nprint,info,
     *                   nfev,fjac,ldfjac,ipvt,qtf,wa1,wa2,wa3,wa4)
                call system_clock(count2,count_rate,count_max)
                r=(count2-count1)/count_rate

                write(4,*)'Tempo de resol. (2a etapa) c/ LMDIF:',r,' s'
                write(4,*)'info: ',info,'  nfev: ',nfev

            else if(metodo.eq.2)then

                write(4,*)'_______________________________________'
                write(4,*)'--L-M - J:DirectDifferentiation--'
                write(4,*)'_______________________________________'   

                call system_clock(count1,count_rate,count_max)

                call lmder(fcn2,m,numpar,x,fvec,fjac,ldfjac,ftol,xtol,
     *                 gtol,maxfev,diag,mode,factor,nprint,info,nfev,
     *                 njev,ipvt,qtf,wa1,wa2,wa3,wa4)
                call system_clock(count2,count_rate,count_max)
                r=(count2-count1)/count_rate

                write(4,*)'Tempo de resol. (2a etapa)  c/ LMDER:',r,' s'
                write(4,*)'info: ',info,'  nfev: ',nfev,'  njev:',njev

            endif	

        endif !fim da segunda parte da otimizacao

 5	format(i5,54f10.5)

	close(4) ! arquivo xxxx.dop.out
	close(FRES)


	
	open(1,file='pgeom.tmp')
	read(1,*)FILEPLD
	read(1,*)g1,g2,g3,g4,g5,g6,g7
	close(1)
	
	open(1,file='fobj0.tmp')
	read(1,*)fobj0
	close(1)
	
	open(1,file='fobjf.tmp')
	read(1,*)fobjf
	close(1)
	
	open(1,file='auxerro.tmp')
	FILEB=FILE_DEX
	write(1,*)FILEB//'-00.pld' !alvo
	write(1,*)FILEPLD !resultado	
	close(1)
	
	
	
	call system('~/Documents/local_codes/erro-curvas/erro-geom')
	
	open(1,file='errog'//FILEPLD//'.tmp')
	read(1,*)errogeom
	close(1)
	
	open(1,file='snr.tmp')
	read(1,*)snr
	close(1)
	
	open(1,file='resum-'//FILE_OPT//'.txt')
	write(1,*)snr,g1,g2,g3,g4,g5,g6,g7,fobj0,fobjf,errogeom
	write(1,*)'snr,x,y,dc,area,perim,Ju,Jv,fobj0,fobjf,errogeom'
	close(1)
	
	call system('mv '//FILEPLD//' resultpld.tmp')!troca o nome dos arqs
	call system('mv '//FILEB//'-00.pld alvopld.tmp')	
	call system('rm *.pld ')
	call system('rm *.plt ')
	call system('mv resultpld.tmp '//FILEPLD) !retorna os arqs pros nomes originais
	call system('mv alvopld.tmp '//FILEB//'-00.pld')	
	
	
	call system('gzip -f '//FILE_RES)
	call system('rm '//FILE_RES//'.gz') !opcional: remove o .gz, para economizar espaço em disco (so para dados com ruido)
	call system('rm *.tmp ')


	stop
	end

